package com.example;

/**
 * @desc:
 * @author: Shoko
 * @date: 2025/6/13
 */
// 4. 前向迭代器（从数组头部 -> 尾部遍历）
public class ForwardIterator implements Iterator {
    private int index;
    private final ProductAggregate aggregate;

    public ForwardIterator(ProductAggregate aggregate) {
        this.aggregate = aggregate;
        this.first(); // 初始化时定位到第一个元素
    }

    @Override
    public void currentItem() {
        String product = aggregate.getProduct(index);
        System.out.println("当前商品：" + product);
    }

    @Override
    public void first() {
        index = 0; // 前向遍历从数组下标 0 开始
    }

    @Override
    public boolean isDone() {
        return index >= aggregate.getCount();
    }

    @Override
    public void next() {
        if (!isDone()) {
            index++;
        }
    }

    @Override
    public void prev() {
        if (index > 0) {
            index--;
        }
    }
}
